package com.zcckj.demo.storage.provider;

import com.zcckj.demo.storage.model.dto.ReduceStockDTO;
import com.zcckj.demo.storage.model.entity.Product;
import com.zcckj.demo.storage.service.ProductService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

/**
 *
 * <p>
 *  TODO 类作用描述
 * </p>
 *
 * @author zhangjie
 * @since 2019-08-28
 */
@SuppressWarnings("Duplicates")
@Service(group = "${dubbo.services.group}",version = "${dubbo.services.version}")
public class StorageWriteServiceImpl  implements StorageWriteService {
    @Autowired
    private ProductService productService;

    @Override
    public Boolean reduceStock(ReduceStockDTO reduceStockDTO) {
        Assert.notNull(reduceStockDTO.getProductId(),"productId cannot be null");
        Assert.notNull(reduceStockDTO.getAmount(),"amount cannot be null");
        Product product = productService.getById(reduceStockDTO.getProductId());
        if(product.getStock().compareTo(reduceStockDTO.getAmount()) < 0){
            throw new RuntimeException("库存不足");
        }
        product.setStock(product.getStock()- reduceStockDTO.getAmount());
        return productService.updateById(product);
    }
}
